package iterator_test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IteratorRemove {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        Iterator<String> iterator = list.iterator();
        Iterator<String> iterator2 = list.iterator();
        System.out.println(iterator.next());
        iterator.remove();
        System.out.println(iterator2.next()); //ArrayList的modCount变化，iterator2的expectedModCount没有变化
//        iterator.remove();
        /**
         * 删除的是lastRet指向的元素，lastRet是最新遍历的元素
         */
        //public void remove() {
        //            if (lastRet < 0)
        //                throw new IllegalStateException();
        //            checkForComodification();
        //
        //            try {
        //                ArrayList.this.remove(lastRet);
        //                cursor = lastRet;
        //                lastRet = -1;
        //                expectedModCount = modCount;
        //            } catch (IndexOutOfBoundsException ex) {
        //                throw new ConcurrentModificationException();
        //            }
        System.out.println(iterator.next());
    }

}
